package com.ssb.admin.modules.sys.role.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ssb.admin.common.utils.R;
import com.ssb.admin.modules.sys.role.controller.dto.RoleQuery;
import com.ssb.admin.modules.sys.role.dao.SysRoleDao;
import com.ssb.admin.modules.sys.role.entity.SysRole;
import com.ssb.admin.modules.sys.role.service.ISysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author duaichao
 * @since 2018-08-23
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements ISysRoleService {
    @Override
    public R queryPage(RoleQuery dto) {
        int limit = dto.getLimit();
        int offset = (dto.getPage() - 1) * limit;
        List<SysRole> sysRoles = this.baseMapper.selectByFilter(offset, limit, dto);
        int total = this.baseMapper.selectByFilterCount(dto);
        return R.ok().list(total, sysRoles);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysRole sysRole) {
        if (sysRole.getId()==null) {
            sysRole.setCreateTime(new Date());
            sysRole.setUpdateTime(new Date());
            this.baseMapper.insert(sysRole);
        }else{
            sysRole.setUpdateTime(new Date());
            this.baseMapper.updateById(sysRole);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void del(SysRole sysRole) {
        this.baseMapper.deleteById(sysRole);
    }
}
